package com.turman.common.db

import androidx.room.*
import androidx.room.ForeignKey.SET_DEFAULT

@Entity(tableName = "db_user", foreignKeys = [ForeignKey(
    entity = Book::class,
    parentColumns = ["bid"],childColumns = ["bookId"], onDelete = SET_DEFAULT
)], indices = [Index("uid"), Index("bookId")])
class DbUser {
    @PrimaryKey(autoGenerate = true)  //主键 自增
    var uid = 0

    @ColumnInfo(name="uname")  //这里指定数据库字段名，默认不指定和类属性名一致
    var name:String? = null

    var city:String? = null

    var age = 0

    //标记该字段不会保存到数据库表中
    @Ignore
    var isSingle=false

    @Embedded    //这个类的字段会建立在user表中，不会创建两个表
    var baby:Child? = null //孩子

    var bookId:Int = 0

    var workInfo:WorkInfo? = null

    override fun toString(): String {
        return "DbUser{" +
                "uid=" + uid +
                ",name=" + name +
                ",city=" + city +
                ",age=" + age +
                ",single=" + isSingle +
                "}"
    }
}

data class Child(
    val cid:Int,
    val cname:String,
    val cAge:Int,
    val sex:Int
)

data class WorkInfo(
    val wName:String?,
    val wSalary:Double
)

@Entity
data class Book(
    @PrimaryKey
    val bid:Int,
    val name:String,
    val price:Double
)

//该数据是sql查询结果数据，可用于dao操作,需要在database注解中声明
@DatabaseView("select uname,name from db_user,book where uid=3 or bookId=3",viewName = "tmpBean")
class TempBean{
    var uname = ""
    var name = ""

    override fun toString(): String {
        return "TempBean(uname='$uname',name='$name')"
    }
}


















